"use strict";
cc._RF.push(module, 'ed152TO/91AB5kenqdReZR1', 'room');
// scripts/room.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.roomManager = exports.userGameInfo = void 0;
var _ = require("lodash");
var common = require("./common");
var userInfo_1 = require("./userInfo");
var resource_1 = require("./resource");
var log = console.log;
var cardManager_1 = require("./cardManager");
var userGameInfo = /** @class */ (function () {
    function userGameInfo(info, position) {
        this.latsOutCardInfo = [];
        this.Avatar = cc.instantiate(cc.find("Canvas").getComponent(resource_1.default).userInfo).getComponent(userInfo_1.default);
        this.Avatar.init(info, position);
        var leftCntPosition;
        var outCardPosition;
        switch (position) {
            case 'left':
                outCardPosition = cc.v2(-190, 50);
                leftCntPosition = cc.v2(-320, 10);
                break;
            case 'center':
                outCardPosition = cc.v2(0, -80);
                leftCntPosition = cc.v2(1000, 1000);
                break;
            case 'right':
                outCardPosition = cc.v2(190, 50);
                leftCntPosition = cc.v2(320, 10);
                break;
        }
        this.outCard = new cardManager_1.cardsShower(outCardPosition);
        this.leftCnt = cc.instantiate(cc.find("Canvas").getComponent(resource_1.default).cardleft);
        cc.find("Canvas").addChild(this.leftCnt);
        this.leftCnt.setPosition(leftCntPosition);
        this.leftCnt.active = false;
        this.outText = this.showAt(outCardPosition, "pass");
        this.outText.node.active = false;
    }
    userGameInfo.prototype.playOut = function (userinfo) {
        var cardinfo = userinfo.outCards;
        if (!cardinfo)
            return;
        if (!cardinfo.length) {
            this.playPass();
            return;
        }
        var notsame = _.xor(cardinfo.map(function (item) { return item.id; }), this.latsOutCardInfo.map(function (item) { return item.id; })).length;
        this.latsOutCardInfo = cardinfo;
        if (notsame) {
            if (cardinfo.length) {
                this.outCard.anchor.active = true;
                this.outText.node.active = false;
                this.outCard.display(cardinfo);
            }
            this.leftCnt.getChildByName("number").getComponent(cc.Label).string = userinfo.handCards.length.toString();
        }
    };
    userGameInfo.prototype.playPass = function () {
        this.outCard.anchor.active = false;
        this.outText.node.active = true;
        this.outText.string = 'pass';
    };
    userGameInfo.prototype.prepared = function () {
        this.outCard.anchor.active = false;
        this.outText.node.active = true;
        this.outText.string = 'prepared';
    };
    userGameInfo.prototype.grapCard = function (grap) {
        if (grap >= 0) {
            this.outCard.anchor.active = false;
            this.outText.node.active = true;
            this.outText.string = grap ? 'grap' : 'pass';
        }
    };
    userGameInfo.prototype.updateLeftCnt = function (userinfo) {
        this.leftCnt.active = true;
        this.leftCnt.getChildByName("number").getComponent(cc.Label).string = userinfo.handCards.length.toString();
    };
    userGameInfo.prototype.destroy = function () {
        cc.find("Canvas").removeChild(this.leftCnt);
        cc.find("Canvas").removeChild(this.Avatar.node);
        this.outCard.destroy();
        this.outText.destroy();
    };
    userGameInfo.prototype.showAt = function (position, text) {
        var node = new cc.Node();
        var label = node.addComponent(cc.Label);
        label.font = cc.find("Canvas").getComponent(resource_1.default).font;
        label.fontSize = 36;
        label.string = text;
        label.node.setPosition(position);
        cc.find("Canvas").addChild(label.node);
        return label;
    };
    return userGameInfo;
}());
exports.userGameInfo = userGameInfo;
var puke = require("./pukeData");
var that;
var roomManager = /** @class */ (function () {
    function roomManager() {
        this.usersInfo = {};
        this.roomInfo = { lastOut: null, dzid: null };
        this.dzCard = new cardManager_1.cardsShower(cc.v2(0, -35 + cc.view.getVisibleSize().height / 2));
        this.handCard = new cardManager_1.handCardManager();
        that = this;
    }
    roomManager.get = function () {
        if (this._ins == null)
            this._ins = new roomManager();
        return this._ins;
    };
    roomManager.prototype.receive = function (data) {
        that.roomInfo = data.roomInfo;
        switch (data.event) {
            case 'joinPlayer':
                that.joinPlayer(data.roomInfo.players);
                break;
            case 'preparePlayer':
                that.preparePlayer(data.roomInfo.players);
                break;
            case 'handOutCards':
                that.handOutCards(data.roomInfo.players);
                break;
            case 'grabCard':
                that.grabCard(data.roomInfo.players);
                break;
            case 'grabCardRes':
                that.grabCardRes(data.roomInfo);
                break;
            case 'playOut':
                for (var uid in data.roomInfo.players)
                    that.usersInfo[uid].playOut(data.roomInfo.players[uid]);
                break;
            case 'gameOver':
                log("game over");
                break;
        }
    };
    roomManager.prototype.joinPlayer = function (users) {
        var playerIdx = Object.keys(users).indexOf(common.uid);
        var usersid = Object.keys(users);
        for (var i = 0; i < usersid.length; i++) {
            var position = void 0;
            if (i == playerIdx)
                position = 'center';
            else if (i == (playerIdx + 2) % 3)
                position = 'right';
            else
                position = 'left';
            if (this.usersInfo[usersid[i]] == undefined)
                this.usersInfo[usersid[i]] = new userGameInfo(users[usersid[i]], position);
        }
        this.preparePlayer(users);
        // _.xor(Object.keys(this.usersInfo), usersid).map(uid=>{
        //     this.usersInfo[uid].destroy()
        //     delete this.usersInfo[uid]
        // })
    };
    roomManager.prototype.preparePlayer = function (users) {
        for (var uid in users)
            if (users[uid].prepared)
                this.usersInfo[uid].prepared();
    };
    roomManager.prototype.handOutCards = function (users) {
        for (var uid in users) {
            this.usersInfo[uid].outText.node.active = false;
            this.usersInfo[uid].updateLeftCnt(users[uid]);
            if (uid == common.uid)
                this.handCard.display(users[uid].handCards);
        }
    };
    roomManager.prototype.grabCard = function (users) {
        for (var uid in users)
            this.usersInfo[uid].grapCard(users[uid].grabRes);
    };
    roomManager.prototype.grabCardRes = function (room) {
        var _this = this;
        var users = room.players;
        this.dzCard.display(room.dzCard);
        Object.keys(this.usersInfo).map(function (uid) {
            _this.usersInfo[uid].outText.node.active = false;
        });
        if (room.dzid == common.uid)
            this.handCard.display(users[common.uid].handCards);
        else
            this.usersInfo[room.dzid].updateLeftCnt(users[room.dzid]);
        this.usersInfo[this.roomInfo.dzid].Avatar.showHat();
    };
    roomManager.prototype.userPlayOut = function () {
        var outCards = this.handCard.getPlayOut();
        var outStyle = puke.outCardCheck(outCards);
        if (!outStyle) {
            common.alert('出牌不符合要求', this.handCard.anchor);
            return null;
        }
        if (this.roomInfo.lastOut) {
            var lastOutStyle = this.roomInfo.lastOut.outStyle;
            if (lastOutStyle.type != outStyle.type) {
                if (outStyle.type != '炸弹') {
                    common.alert('出牌不符合要求', this.handCard.anchor);
                    return null;
                }
            }
            else if (lastOutStyle.grade >= outStyle.grade) {
                common.alert('出牌不符合要求', this.handCard.anchor);
                return null;
            }
        }
        this.handCard.playOut();
        var param = {
            outStyle: outStyle, outCards: outCards
        };
        return param;
    };
    return roomManager;
}());
exports.roomManager = roomManager;

cc._RF.pop();